package com.pay.ioopos.activity;

import android.content.Intent;
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.PwdListener;
import com.pay.ioopos.R;
import com.pay.ioopos.ScanListener;
import com.pay.ioopos.StatusListener;
import com.pay.ioopos.fragment.ComPwdValidFragment;
import com.pay.ioopos.fragment.ShopBindFragment;
import com.pay.ioopos.fragment.ShopBindScanFragment;
import com.pay.ioopos.fragment.ShopInfoFragment;
import com.pay.ioopos.fragment.ShopMenuFragment;
import com.pay.ioopos.fragment.ShopShiftFragment;
import com.pay.ioopos.fragment.ShopStaffIdFragment;
import com.pay.ioopos.fragment.ShopStaffIdFragment.OnStaffIdListner;
import com.pay.ioopos.fragment.ShopUnbindFragment;
import com.pay.ioopos.fragment.TipVerticalFragment;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.KeyInfo.KEY_NUM_1;
import static com.pay.ioopos.KeyInfo.KEY_NUM_2;
import static com.pay.ioopos.KeyInfo.KEY_NUM_3;
import static com.pay.ioopos.KeyInfo.KEY_NUM_4;
import static com.pay.ioopos.fragment.TipFragment.TipType.FAIL;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;

public class ShopActivity extends AbstractActivity implements ScanListener, PwdListener, OnStaffIdListner, StatusListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
        setMainFragment(new ShopMenuFragment());
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {

        if (mainFragment instanceof ShopMenuFragment) {
            if (keyInfo == KEY_NUM_1) {
                setMainFragment(new TipVerticalFragment(FAIL, "功能未开通"));
                //setMainFragment(new ShopStaffIdFragment(this));
            } else if (keyInfo == KEY_NUM_2) {
                setMainFragment(new ShopInfoFragment());
            } else if (keyInfo == KEY_NUM_3) {
                setMainFragment(new ShopBindFragment(this));
                //setMainFragment(new ShopBindScanFragment(this));
            } else if (keyInfo == KEY_NUM_4) {
                ComPwdValidFragment pwdFragment = new ComPwdValidFragment();
                pwdFragment.setPwdListener(this);
                setMainFragment(pwdFragment);
            } else if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                Intent intent = new Intent(this, SettingActivity.class);
                startActivity(intent);
            }
        } else if (mainFragment instanceof ShopStaffIdFragment
                || mainFragment instanceof ShopBindFragment
                || mainFragment instanceof ShopUnbindFragment
                || mainFragment instanceof ShopShiftFragment) {

        } else if (mainFragment instanceof ShopBindScanFragment) {
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        } else if (mainFragment instanceof ComPwdValidFragment) {
            ComPwdValidFragment fragment = ((ComPwdValidFragment)mainFragment);
            fragment.onKeyDown(keyInfo);
            if (keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        } else if (mainFragment instanceof ShopInfoFragment
                || mainFragment instanceof TipVerticalFragment) {
            if (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL) {
                setMainFragment(new ShopMenuFragment());
            }
        }
        return false;
    }

    @Override
    public boolean onScan(String code) {
        if (mainFragment instanceof ShopBindScanFragment) {
            ShopBindFragment fragment = new ShopBindFragment(code, this);
            setMainFragment(fragment);
        }
        return true;
    }

    @Override
    public void onPwdValid(String pwd) {
        ShopUnbindFragment fragment = new ShopUnbindFragment(this);
        setMainFragment(fragment);
    }

    @Override
    public void onStaffIdFinish(String staffId) {
        Fragment fragment = new ShopShiftFragment(staffId, this);
        setMainFragment(fragment);
    }

    @Override
    public void onStaffIdCancel() {
        setMainFragment(new ShopMenuFragment());
    }

    @Override
    public void onSuccess(String msg) {
        setMainFragment(new TipVerticalFragment(SUCCESS, msg));
    }

}
